import {onMounted, reactive, ref,} from 'vue'
import mtUtil from "@/utils/mtUtil";
import validate from 'ele-admin-pro/packages/validate';

export default function AccountEditMap(props) {

  let defaultContact = {
    name: "",
    status: "",
    remark: "",
    creatorId: null,
    creatorName: "",
    createDatetime: "",
    entityName: "",
    businessId: null,
    telephone: "",
    landline: null,
    email: null,
    wechat: null,
    jobTitle: "",
    address: null,
    eid: null
  }
  const contact = reactive(props.contactEntity.name === undefined ? defaultContact : props.contactEntity);

  let dictOption = mtUtil.getDict("往来单位联系人职务").value
  let AddressEntityData = reactive({});
  let AddressStatus = ref("");

  if (contact.address !== null) {
    AddressStatus.value = contact.address.status
    AddressEntityData = contact.address
  } else {
    console.log(contact)
    AddressStatus.value = "停用";
    AddressEntityData = {
      addressAbbreviation: "",
      addressType: "",
      businessID: null,
      detailedAddress: "",
      province: "",
      city: "",
      district: "",
      streetAddress: "",
      addressLongitude: 0,
      addressLatitude: 0,
      zipCode: "",
    };
  }

  //获取父组件传递过来的联系人信息
  onMounted(() => {
  })


  // 实现select选择框可下拉单选，也可输入赋值
  const handleSearch = (value) => {
    handleChange(value);
  }
  const handleChange = (value) => {
    contact.jobTitle = value != null ? value : '';
  }

  //失焦时获取新增的对象
  const handleBlur = () => {
    console.log(contact.jobTitle)
    dictOption = mtUtil.getDict("往来单位联系人职务")
    dictOption.push(contact.jobTitle)
  }


  const getContact = () => {
    if (contact.name === undefined) {
      return defaultContact
    }else {
      console.log(contact)
      return contact
    }

  }


  if (contact.landline === null) {
    contact.landline = ""
  }

  if (contact.wechat === null) {
    contact.wechat = ""
  }
  if (contact.email === null) {
    contact.email = ""
  }
  //获取子组件传递过来的表单数据。
  let getChildData = (formData) => {
    console.log(formData);
    contact.address = formData;
    console.log(contact)
    return formData;
  };


  const rulesRef = reactive({
    name: [{
      required: true,
      message: "请输入联系人姓名",
      trigger: 'onSubmit'
    }],
    telephone: [
      {
        required: true,
        message: "请输入手机",
        trigger: 'onSubmit'
      },
      {
        pattern: validate.phone,
        message: "请输入正确的电话",
        trigger: 'onSubmit'
      }
    ],
    landline: [
      {
        pattern: validate.tel,
        message: "请输入正确的座机号码",
        trigger: 'onSubmit'
      }
    ],
    email: [
      {
        pattern: validate.email,
        message: "请输入正确的邮箱",
        trigger: 'onSubmit'
      }
    ],
    wechat: [
      {
        //微信官方定义的微信号规则
        //
          // 1、可使用6-20个字母、数字、下划线和减号；
          //
          // 2、必须以字母开头（字母不区分大小写）；
          //
          // 3、不支持设置中文。
        pattern: "/^[a-zA-Z][a-zA-Z\\d_-]{5,19}$/",
        message: "请输入正确的微信号",
        trigger: 'onSubmit'
      }
    ],
    jobTitle: [],
    address: [],
  });

  return {
    rulesRef,
    handleSearch,
    handleChange,
    handleBlur,
    dictOption,
    contact,
    getContact,
    AddressEntityData,
    AddressStatus,
    getChildData,
  }
}
